package com.joyplus.report.business.controller;

import com.alibaba.fastjson.JSON;
import com.joyplus.report.business.domain.MdCampaignSupplement;
import com.joyplus.report.business.qo.MdCampaignSupplementQO;
import com.joyplus.report.business.service.C2SMdCampaignService;
import com.joyplus.report.business.service.MdCampaignSupplementService;
import com.joyplus.report.business.service.OptimizationTaskStatisticsService;
import com.joyplus.report.common.utils.DateUitls;
import com.joyplus.report.core.Result;
import com.joyplus.report.core.ResultGenerator;
import com.github.pagehelper.PageInfo;
import com.mongodb.DBObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

/**
 * Controller
 *
 * @author jeff
 * @date 2018-05-31
 */
@RestController
@Api(tags = "")
@RequestMapping("/md/campaign/c2s")
public class C2SMdCampaignController {

  @Resource
  private C2SMdCampaignService c2SMdCampaignService;

  @Resource
  private OptimizationTaskStatisticsService optimizationTaskStatisticsService;

  @GetMapping("/c2sdetail")
  @ApiOperation(value = "查询详情", response = MdCampaignSupplement.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "long"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result c2sdetail(@RequestParam String id,
      @RequestParam(required = false, name = "startDate", defaultValue = "") String startDate,
      @RequestParam(required = false, name = "endDate", defaultValue = "") String endDate) {

    List planCampaign = optimizationTaskStatisticsService.getTaskStatisticsByDate(id.split(","), startDate, endDate);
    List c2SRealCampaign = c2SMdCampaignService
        .getC2SRealCampaign(id.split(","), startDate, endDate);

    List optimizationTaskFullFreq = optimizationTaskStatisticsService
        .getOptimizationTaskFullFreq(id.split(","));
    List<String> dates = DateUitls.findDates(startDate, endDate);
    Map<Object, Object> objectObjectMap = new HashMap<>();
    objectObjectMap.put("planCampaign", JSON.toJSON(planCampaign));
    objectObjectMap.put("c2SRealCampaign", JSON.toJSON(c2SRealCampaign));
    objectObjectMap.put("fullFreq", JSON.toJSON(optimizationTaskFullFreq));
    objectObjectMap.put("dates", JSON.toJSON(dates));
    return ResultGenerator.genSuccessResult(objectObjectMap);
  }


  @GetMapping("/getC2SFre")
  @ApiOperation(value = "获取频次", response = MdCampaignSupplement.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result getC2SFre(@RequestParam String id,
      @RequestParam(required = false, name = "startDate", defaultValue = "") String startDate,
      @RequestParam(required = false, name = "endDate", defaultValue = "") String endDate) {

    Map<Object, Object> objectObjectMap = new HashMap<>();
    List c2SFre = c2SMdCampaignService.getC2SFre(id.split(","), startDate);
    List taskStatisticsByFre = optimizationTaskStatisticsService
        .getTaskStatisticsByFre(id.split(","), startDate);

    objectObjectMap.put("c2SFre", JSON.toJSON(c2SFre));
    objectObjectMap.put("taskStatisticsByFre", JSON.toJSON(taskStatisticsByFre));
    return ResultGenerator.genSuccessResult(objectObjectMap);
  }

  @GetMapping("/getC2SHour")
  @ApiOperation(value = "分时", response = MdCampaignSupplement.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result getC2SHour(@RequestParam String id,
      @RequestParam(required = false, name = "startDate", defaultValue = "") String startDate,
      @RequestParam(required = false, name = "endDate", defaultValue = "") String endDate) {

    Map<Object, Object> objectObjectMap = new HashMap<>();
    List taskStatisticsByHour = optimizationTaskStatisticsService
        .getTaskStatisticsByHour(id.split(","), startDate, endDate);
    List c2SHour = c2SMdCampaignService.getC2SHour(id.split(","), startDate, endDate);
    objectObjectMap.put("optimizationTask", taskStatisticsByHour);
    objectObjectMap.put("c2SHour", c2SHour);
    return ResultGenerator.genSuccessResult(objectObjectMap);
  }


  @GetMapping("/getC2SHourList")
  @ApiOperation(value = "分时", response = MdCampaignSupplement.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result getC2SHourList(@RequestParam String id,
      @RequestParam(required = false, name = "startDate", defaultValue = "") String startDate,
      @RequestParam(required = false, name = "endDate", defaultValue = "") String endDate) {

    Map<Object, Object> objectObjectMap = new HashMap<>();
    List planCampaign = optimizationTaskStatisticsService.getTaskStatisticsByDate(id.split(","), startDate, endDate);
    List c2SRealCampaign = c2SMdCampaignService
        .getC2SRealCampaign(id.split(","), startDate, endDate);
    objectObjectMap.put("optimizationTask", JSON.toJSON(planCampaign));
    objectObjectMap.put("c2SHourList", JSON.toJSON(c2SRealCampaign));
    return ResultGenerator.genSuccessResult(objectObjectMap);
  }


  @GetMapping("/getC2SLocation")
  @ApiOperation(value = "地域", response = MdCampaignSupplement.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result getC2SLocation(@RequestParam String id,
      @RequestParam(required = false, name = "startDate", defaultValue = "") String startDate,
      @RequestParam(required = false, name = "endDate", defaultValue = "") String endDate) {

    Map<Object, Object> objectObjectMap = new HashMap<>();
    List c2sbylocation = c2SMdCampaignService.getC2sByLocation(id.split(","), startDate, endDate);
    List taskStatisticsByLocation = optimizationTaskStatisticsService
        .getTaskStatisticsByLocation(id.split(","), startDate, endDate);

    List optimizationFull = optimizationTaskStatisticsService.getTaskStatisticsByLocationFull(id);

    MdCampaignSupplement campaignSupplement = c2SMdCampaignService.getById(Long.parseLong(id));
    objectObjectMap.put("mapData", c2sbylocation);
    objectObjectMap.put("optimization", taskStatisticsByLocation);
    objectObjectMap.put("optimizationFull",optimizationFull);
    objectObjectMap.put("campaignSupplement",campaignSupplement);
    return ResultGenerator.genSuccessResult(objectObjectMap);
  }




  @GetMapping("/list")
  @ApiOperation(value = "分页查询", response = MdCampaignSupplement.class, notes = "分页查询")
  public Result list(MdCampaignSupplementQO mdCampaignSupplementQO) {
    List<MdCampaignSupplement> p = c2SMdCampaignService.findC2SList();
    return ResultGenerator.genSuccessResult(p);
  }

  @GetMapping("/getGeoListByProvince")
  @ApiOperation(value = "查询详情", response = MdCampaignSupplement.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "long"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = true, name = "province", value = "省份", paramType = "query", dataType = "String")
  })
  public Result getGeoListByProvince(@RequestParam String id,
      @RequestParam(required = false, name = "startDate", defaultValue = "") String startDate,
      @RequestParam(required = false, name = "endDate", defaultValue = "") String endDate,
      @RequestParam String province) {
    List geoListByProvince = c2SMdCampaignService
        .getGeoListByProvinceFull(id.split(","), startDate, endDate, province);
    List taskStatisticsByProvince = optimizationTaskStatisticsService
        .getTaskStatisticsByProvince(id.split(","), startDate, endDate, province);
    List c2SLocationFull = c2SMdCampaignService.getC2SLocationFull(id);
    HashMap<Object, Object> map = new HashMap<>();
    map.put("c2s", JSON.toJSON(geoListByProvince));
    map.put("optimization", JSON.toJSON(taskStatisticsByProvince));
    map.put("c2SLocationFull", JSON.toJSON(c2SLocationFull));
    return ResultGenerator.genSuccessResult(map);
  }


}
